package com.xdcplus.web.service.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.DwWorkConfMapper;
import com.xdcplus.web.common.pojo.entity.DwWorkConf;
import com.xdcplus.web.common.pojo.dto.DwWorkConfDTO;
import com.xdcplus.web.common.pojo.dto.DwWorkConfFilterDTO;
import com.xdcplus.web.common.pojo.vo.DwWorkConfVO;
import com.xdcplus.web.common.pojo.query.DwWorkConfQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.DwWorkConfService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * 执行任务计划(DwWorkConf)表服务实现类
 *
 * @author ma.qing
 * @since 2023-07-06 18:24:55
 */
@Slf4j
@Service("dwWorkConfService")
public class DwWorkConfServiceImpl extends BaseServiceImpl<DwWorkConfMapper, DwWorkConf, DwWorkConf, DwWorkConfVO> implements DwWorkConfService {

    @Autowired
    protected DwWorkConfMapper dwWorkConfMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveDwWorkConf(DwWorkConfDTO dwWorkConfDTO) {

        DwWorkConf dwWorkConf = new DwWorkConf();
        BeanUtil.copyProperties(dwWorkConfDTO, dwWorkConf);
        dwWorkConf.setCreatedTime(new Timestamp(DateUtil.current()));

        return this.save(dwWorkConf);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateDwWorkConf(DwWorkConfDTO dwWorkConfDTO) {

        DwWorkConf dwWorkConf = this.getById(dwWorkConfDTO.getId());
        if (ObjectUtil.isNull(dwWorkConf)) {
            log.error("updateDwWorkConf() The DwWorkConf does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(dwWorkConfDTO, dwWorkConf);
        dwWorkConf.setUpdatedUser(dwWorkConfDTO.getUpdatedUser());
        dwWorkConf.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(dwWorkConf);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<DwWorkConf> dwWorkConfList) {

        dwWorkConfList.forEach(dwWorkConf -> {
            DwWorkConf dwWorkConfParam = new DwWorkConf();
            dwWorkConfParam.setId(dwWorkConf.getId());
            if (ObjectUtil.isNotNull(dwWorkConf.getId())) {
                dwWorkConf.setId(dwWorkConf.getId());
                dwWorkConf.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<DwWorkConf> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(DwWorkConf::getId, dwWorkConf.getId());
                update(dwWorkConf, lambdaUpdate);
            } else {
                dwWorkConf.setCreatedTime(new Timestamp(DateUtil.current()));
                save(dwWorkConf);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<DwWorkConfDTO> dwWorkConfDTOList) {

        List<DwWorkConf> dwWorkConfList = BeanUtils.copyProperties(dwWorkConfDTOList, DwWorkConf.class);
        return saveOrUpdateBatch(dwWorkConfList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteDwWorkConfLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        DwWorkConf dwWorkConf = this.getById(id);

        if (ObjectUtil.isNull(dwWorkConf)) {
            log.error("deleteDwWorkConf() The DwWorkConf does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteDwWorkConfByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
        List<String> idList = Arrays.asList(ids.split(","));
        if(CollectionUtil.isEmpty(idList)){
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        idList.forEach(id->{
            DwWorkConf dwWorkConf = this.getById(id);
            if (ObjectUtil.isNull(dwWorkConf)) {
                log.error("deleteDwWorkConf() The DwWorkConf does not exist or has been deleted");
                throw new XdcWebException(ResponseEnum.ERROR);
            }
        });

        return this.removeByIds(idList);
    }

    private List<DwWorkConf> queryDwWorkConfList(DwWorkConfFilterDTO dwWorkConfFilterDTO) {
        DwWorkConfQuery dwWorkConfQuery = BeanUtil.copyProperties(dwWorkConfFilterDTO, DwWorkConfQuery.class);

        return dwWorkConfMapper.queryDwWorkConf(dwWorkConfQuery);
    }

    @Override
    public List<DwWorkConfVO> queryDwWorkConfVOList(DwWorkConfFilterDTO dwWorkConfFilterDTO) {
        return this.objectConversion(queryDwWorkConfList(dwWorkConfFilterDTO));
    }

    @Override
    public PageVO<DwWorkConfVO> queryDwWorkConf(DwWorkConfFilterDTO dwWorkConfFilterDTO) {
        PageVO<DwWorkConfVO> pageVO = new PageVO<>();

        if (dwWorkConfFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(dwWorkConfFilterDTO.getCurrentPage(), dwWorkConfFilterDTO.getPageSize(),
                    dwWorkConfFilterDTO.getOrderType(), dwWorkConfFilterDTO.getOrderField());
        }

        List<DwWorkConf> dwWorkConfList = queryDwWorkConfList(dwWorkConfFilterDTO);

        PageInfo<DwWorkConf> pageInfo = new PageInfo<>(dwWorkConfList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(dwWorkConfList));

        return pageVO;
    }

    @Override
    public DwWorkConfVO queryDwWorkConfById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public DwWorkConf queryDwWorkConfByEtlName(String etlName) {

        Assert.notNull(etlName, "任务名称不能为空");

        return dwWorkConfMapper.queryDwWorkConfByEtlName(etlName);
    }


    @Transactional
    @Override
    public void updateConfById(DwWorkConf dwWorkConf) {
        baseMapper.updateConfById(dwWorkConf.getId(),
                dwWorkConf.getEtlStartTimestamp(),
                dwWorkConf.getEtlEndTimestamp());
    }


}
